package address

import (
	"OnlineRetailers/controllers/errorDeal"
	"OnlineRetailers/controllers/login"
	USER "OnlineRetailers/models/User"
	"OnlineRetailers/models/User/resMes"
	error2 "OnlineRetailers/models/errorDeal"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

type GetAddressById struct {
	beego.Controller
}

func (c *GetAddressById) GetAddressById() {
	defer func() {
		c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
		c.Ctx.Output.Status = 200
		c.ServeJSON()
	}()
	var addr = "/address/GetAddressById"
	res := resMes.GetAddress{}
	address := []*USER.Address{}
	log := &error2.Log{}
	var err error
	o := orm.NewOrm()

	id := c.GetString("id")
	err, openid := login.CheckOpenid(c.Ctx.Request.Header["Openid"])
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}

	sql := "select * from `address` where `address_id`=? and `user_id`=?"
	_, err = o.Raw(sql, id, openid).QueryRows(&address)
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}
	res.Address = address
	res.Code = "200"
	c.Data["json"] = res
}
